package com.github.linkoog.model_designer.ide.editer.dbtable.tab_editer;

import com.github.linkoog.model_designer.ide.type.TableYamlFileType;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorPolicy;
import com.intellij.openapi.fileEditor.FileEditorProvider;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.psi.YAMLFile;

public class DBTableFileEditorProvider implements FileEditorProvider, DumbAware {

    private static final Logger LOG = Logger.getInstance(DBTableFileEditorProvider.class);

    @Override
    public boolean accept(@NotNull final Project project, @NotNull final VirtualFile virtualFile) {
        FileType fileType = virtualFile.getFileType();
        PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        return  psiFile instanceof YAMLFile && fileType instanceof TableYamlFileType;
    }

    @Override
    public FileEditor createEditor(@NotNull final Project project, @NotNull final VirtualFile virtualFile) {
        return new DBTableUIFormEditor(project, virtualFile);
    }

    @Override
    public void disposeEditor(@NotNull final FileEditor editor) {
        Disposer.dispose(editor);
    }

    @Override
    public void writeState(@NotNull final FileEditorState state, @NotNull final Project project, @NotNull final Element element) {
    }

    @Override
    public FileEditorState readState(@NotNull final Element element, @NotNull final Project project, @NotNull final VirtualFile file) {
        return DBTableUIFormEditor.MY_EDITOR_STATE;
    }



    @Override
    public String getEditorTypeId() {
        return "DevToolsDBTableEditer";
    }

    @Override
    @NotNull
    public FileEditorPolicy getPolicy() {
        return FileEditorPolicy.PLACE_BEFORE_DEFAULT_EDITOR;
    }


}
